package controller;

import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.commons.web.JsonResult;
import cn.tedu.csmall.product.pojo.param.CategoryAddNewParam;
import cn.tedu.csmall.product.pojo.param.CategoryUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.*;
import cn.tedu.csmall.product.service.CategoryService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/categories")
@Api(tags = "01. 类别管理模块")
@Slf4j
@Validated
public class CategoryController {

    @Autowired
    private CategoryService service;

    @PostMapping("/add-new")
    @ApiOperation("添加类别")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid CategoryAddNewParam categoryAddNewParam){
        service.addNew(categoryAddNewParam);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除类别")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "Long")
    })
    public JsonResult delete(
            @Range(min = 1, message = "根据ID删除类别失败，请提交合法的ID值！")
            @PathVariable Long id){
        service.deleteById(id);

        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("根据ID更新类别")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "Long")
    public JsonResult updateById(
            @Range(min = 1, message = "根据ID更新类别失败，请提交合法的ID值！")
            @PathVariable Long id,
            @Valid CategoryUpdateInfoParam categoryUpdateInfoParam){
        service.updateInfoById(id, categoryUpdateInfoParam);

        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/enable")
    @ApiOperation("启用类别")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult setEnable(@PathVariable @Range(min = 1, message = "请提交有效的类别ID值！") Long id) {
        service.setEnable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/disable")
    @ApiOperation("禁用类别")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult setDisable(@PathVariable @Range(min = 1, message = "请提交有效的类别ID值！") Long id) {
        service.setDisable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/display")
    @ApiOperation("显示类别")
    @ApiOperationSupport(order = 312)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult setDisplay(@PathVariable @Range(min = 1, message = "请提交有效的类别ID值！") Long id) {
        service.setDisplay(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/hidden")
    @ApiOperation("隐藏类别")
    @ApiOperationSupport(order = 313)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult setHidden(@PathVariable @Range(min = 1, message = "请提交有效的类别ID值！") Long id) {
        service.setHidden(id);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询类别详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "Long")
    })
    public JsonResult getStandardById(
            @Range(min = 1, message = "根据ID查询类别失败，请提交合法的ID值！")
            @PathVariable Long id){
        CategoryStandardVO standardById = service.getStandardById(id);
        return JsonResult.ok(standardById);
    }

    @GetMapping("/list-by-parent")
    @ApiOperation("查询指定父级下的列表")
    @ApiOperationSupport(order = 415)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "albumId", value = "相册ID", required = true, paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "pageNum", value = "页码值", paramType = "query"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
    })
    public JsonResult listByParentId(Long parentId,
                                     @Range(min = 1, message = "查询类别列表失败，请提供正确的页码值!") Integer pageNum,
                                     String queryType){
        if(pageNum == null || pageNum < 1){
            pageNum = 1;
        }
        PageData<CategoryListItemVO> pageData;
        if("all".equals(queryType)){
            pageData = service.listByParentId(parentId, pageNum, Integer.MAX_VALUE);
        }else{
            pageData = service.listByParentId(parentId, pageNum);
        }
        return JsonResult.ok(pageData);
    }

    @GetMapping("/tree")
    @ApiOperation("查询类别树")
    @ApiOperationSupport(order = 420)
    public JsonResult listTree() {
        List<CategoryTreeItemVO> categoryTree = service.listTree();
        return JsonResult.ok(categoryTree);
    }
}
